package sdc.media.motorbike.Controller;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

public class SaveData {

	// Constant
	private Preferences pref;
	private static final String PREF_VOLUME = "volume";
	private static final String PREF_SOUND_ENABLE = "sound.enable";
	private static final String PREF_MUSIC_ENABLE = "music.enable";
	private static final String PREF_NAME = "speed_racing";
	
	private static final String PREF_PLAYER_NAME = "player_name";
	private static final String PREF_PLAYER_SCORE = "player_score";
	
	public SaveData() {
		// TODO Auto-generated constructor stub
	}
	
	protected Preferences getPrefs() {
		// TODO Auto-generated method stub
		if(pref == null)
			pref = Gdx.app.getPreferences(PREF_NAME);
		return pref;
	}
	
	public boolean isSoundEnable() {
		// TODO Auto-generated method stub
		return getPrefs().getBoolean(PREF_SOUND_ENABLE, true);
	}
	
	public void setSoundEnable(boolean soundEnable) {
		// TODO Auto-generated method stub
		getPrefs().putBoolean(PREF_SOUND_ENABLE, soundEnable);
		getPrefs().flush();
	}
	
	public boolean isMusicEnable() {
		// TODO Auto-generated method stub
		return getPrefs().getBoolean(PREF_MUSIC_ENABLE, true);
	}
	
	public void setMusicEnable(boolean musicEnable) {
		// TODO Auto-generated method stub
		getPrefs().putBoolean(PREF_MUSIC_ENABLE, musicEnable);
		getPrefs().flush();
	}
	
	public float getVolume() {
		return getPrefs().getFloat(PREF_VOLUME, 0.5f);
	}
	
	public void setVolume(float volume) {
		// TODO Auto-generated method stub
		getPrefs().putFloat(PREF_VOLUME, volume);
		getPrefs().flush();
	}
	
	public String[] getPlayerName() {
		String[] kq = new String[5];
		for (int i = 0; i < 5; i++) {
			kq[i] = getPrefs().getString(PREF_PLAYER_NAME+(i+1), Setting.names[i]);
		}
		return kq;
	}
	
	public void setPlayerName(String[] name) {
		for (int i = 0; i < name.length; i++) {
			getPrefs().putString(PREF_PLAYER_NAME+(i+1), name[i]);
			getPrefs().flush();
		}
	}
	
	public int[] getPlayerScore() {
		int[] kq = new int[5];
		for (int i = 0; i < 5; i++) {
			kq[i] = getPrefs().getInteger(PREF_PLAYER_SCORE+(i+1), Setting.scores[i]);
		}
		return kq;
	}
	
	public void setPlayerScore(int[] score) {
		for (int i = 0; i < score.length; i++) {
			getPrefs().putInteger(PREF_PLAYER_SCORE+(i+1), score[i]);
			getPrefs().flush();
		}
	}
	
}
